﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;
using WinnerCMS.BLL;
using WinnerCMS.Common;

namespace WinnerCMS.WebFactory.Mvc
{
    /// <summary>
    /// 启用模板切换
    /// </summary>
    public sealed class TemplateRelevantAttribute : ActionFilterAttribute
    {

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var viewResult = filterContext.Result as ViewResult;
            if (viewResult != null)
            {
                if (!viewResult.ViewName.StartsWith("~/Templates/", StringComparison.CurrentCultureIgnoreCase))
                {
                    if (viewResult.ViewName.IsNullOrEmpty())
                    {
                        var controller = filterContext.RequestContext.RouteData.Values["Controller"].ToString();
                        var action = filterContext.RequestContext.RouteData.Values["Action"].ToString();
                        viewResult.ViewName = controller + "/" + action+".aspx";
                    }
                    viewResult.ViewName = string.Format(
                        "~/Templates/{0}/{1}",
                        TemplatesManage.GetUseing.TemplatePath,
                        viewResult.ViewName);

                    return;
                }
            }

            base.OnResultExecuting(filterContext);
        }

    }
}